/*
 * Copyright (c) 2012-2016, b3log.org & hacpai.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */
package com.ld.admin.service.webbot;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ld.shieldsb.common.composition.util.SpiderUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * Turing query service.
 *
 * @author <a href="http://88250.b3log.org">Liang Ding</a>
 * @version 1.0.0.0, May 29, 2016
 * @since 1.0.0
 */
@Slf4j
public class TuringQueryService {

    /**
     * Chat with Turing Robot.
     *
     * @param userName
     *            the specified user name
     * @param msg
     *            the specified message
     * @return robot returned message, return {@code null} if not found
     */
    public String chat(final String userName, String msg) {
        if (StringUtils.isBlank(msg)) {
            return null;
        }

        if (StringUtils.isBlank(userName) || StringUtils.isBlank(msg)) {
            return null;
        }

        try {
            String turingApi = WebBotConfig.getString("turing.api");
            String turingKey = WebBotConfig.getString("turing.key");
            Map<String, String> parms = new HashMap<>();
            parms.put("key", turingKey);
            parms.put("info", msg);
            parms.put("userid", userName);
            String responseContent = SpiderUtil.getRemoteURLContent(turingApi, parms, "post"); // 可以同时设置超时时间

            final JSONObject data = JSON.parseObject(responseContent);
            final int code = data.getIntValue("code");

            switch (code) {
                case 40001:
                case 40002:
                case 40007:
                    log.error(data.getString("text"));

                    return null;
                case 40004:
                    return "聊累了，明天请早吧~";
                case 100000:
                    return data.getString("text");
                case 200000:
                    return data.getString("text") + " " + data.getString("url");
                case 302000:
                    String ret302000 = data.getString("text") + " ";
                    final JSONArray list302000 = data.getJSONArray("list");
                    final StringBuilder builder302000 = new StringBuilder();
                    for (int i = 0; i < list302000.size(); i++) {
                        final JSONObject news = list302000.getJSONObject(i);
                        builder302000.append(news.getString("article")).append(news.getString("detailurl")).append("\n\n");
                    }

                    return ret302000 + " " + builder302000.toString();
                case 308000:
                    String ret308000 = data.getString("text") + " ";
                    final JSONArray list308000 = data.getJSONArray("list");
                    final StringBuilder builder308000 = new StringBuilder();
                    for (int i = 0; i < list308000.size(); i++) {
                        final JSONObject news = list308000.getJSONObject(i);
                        builder308000.append(news.getString("name")).append(news.getString("detailurl")).append("\n\n");
                    }

                    return ret308000 + " " + builder308000.toString();
                default:
                    log.warn("Turing Robot default return [" + data.toString() + "]");
            }
        } catch (final Exception e) {
            log.error("Chat with Turing Robot failed", e);
        }

        return null;
    }
}
